package com.ddyx.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ddyx.common.util.EncrypMD5;
import com.ddyx.common.util.LoginResponse;
import com.ddyx.common.util.ResponseJson;
import com.ddyx.entity.Employe;
import com.ddyx.entity.User;
import com.ddyx.mapper.EmployeMapper;
import com.ddyx.mapper.UserMapper;
import com.ddyx.service.IEmployeService;

@Service("employeService")
public class EmployeServiceImpl implements IEmployeService {

	@Resource
	EmployeMapper employeMapper;
	@Resource
	UserMapper userMapper;
	@Override
	public String selectEmployeBy(Map<String, Object> map) {
		// TODO Auto-generated method stub
		int size = map.get("size") == null ? 0 : Integer.valueOf(map.get("size") + "");
		int page = map.get("page") == null ? 0 : Integer.valueOf(map.get("page") + "");
		int start = 0;
		if (size < 1)
			size = 20;// 默认每页20条
		if (page < 1)
			page = 1;// 默认第1页
		start = (page - 1) * size;
		map.put("start", start);
		map.put("size", size);
		List<Employe> list = employeMapper.getEmploye(map);
		int count =employeMapper.getEmployeCount(map);
		return ResponseJson.getInstance().setPage(page).setSize(size).setTotal(count).setData(list).toString();
	}

	@Transactional
	@Override
	public String addEmploye(JSONObject object) {
		// TODO Auto-generated method stub
		int storeId = object.getIntValue("storeId");
		String employeName = object.getString("employeName");
		String mobile = object.getString("mobile");
		String email = object.getString("email");
		Employe employe = new Employe();
		User user = new User();
		try {
			user.setUserUuid(UUID.randomUUID().toString());
			user.setUserName(employeName);
			user.setPassword(new EncrypMD5().EncoderByMd5("123456"));
			System.out.println(user.getPassword());
			user.setNickName(employeName);
			boolean c = userMapper.insert(user);
			employe.setStoreId(storeId);
			employe.setUserId(user.getUserId());
			employe.setEmployeName(employeName);
			employe.setMobile(mobile);
			employe.setEmail(email);
			boolean b =employeMapper.insert(employe);
			if (!b || !c) {
				return LoginResponse.getInstance().setStatus(false)
						.setMessage("新增店员失败")
						.setData(new JSONArray()) .toString();
			}
		}catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return LoginResponse.getInstance().setStatus(false)
					.setMessage(e.toString())
					.setData(new JSONObject()) .toString();
		}
		return LoginResponse.getInstance().setStatus(true).setMessage("").setData(employe) .toString();
	}

	@Override
	public String editEmploye(JSONObject object) {
		// TODO Auto-generated method stub
		int employeId = object.getIntValue("employeId");
		int storeId = object.getIntValue("storeId");
		String employeName = object.getString("employeName");
		String mobile = object.getString("mobile");
		String email = object.getString("email");
		Employe employe = new Employe();
		try {
			employe.setEmployeId(employeId);
			employe.setStoreId(storeId);
			employe.setEmployeName(employeName);
			employe.setMobile(mobile);
			employe.setEmail(email);
			boolean b =employeMapper.updateByPrimaryKey(employe);
			if(!b) {
				return LoginResponse.getInstance().setStatus(false)
						.setMessage("修改店员失败")
						.setData(new JSONArray()) .toString();
			}
		}catch (Exception e) {
			return LoginResponse.getInstance().setStatus(false)
						.setMessage(e.toString())
						.setData(new JSONArray()).toString();
		}
		return LoginResponse.getInstance().setStatus(true).setMessage("").setData(new JSONArray()) .toString();
	} 

}
